var request = require('superagent'),
  getPassword = require('./getPassword'),
  config = require('./config');

module.exports = function getVerifyCode(opt){

  var params = config.LOGIN_PARAMS;
  params.verifycode = opt.verifycode;
  params.u = opt.qq;
  params.p = getPassword(opt.qq, opt.pass, opt.verifycode);

  if(opt.loginSig){
    params.pt_vcode_v1 = 1;
    params.pt_verifysession_v1 = opt.loginSig;
  };

  var headers = config.HEADERS;
  headers.Cookie = opt.cookies;
  headers.Referer = config.LOGIN_REFERER;
  headers.Host = config.LOGIN_HOST;

  request.get(config.LOGIN_HOST + config.LOGIN_PATH)
    .query(params)
    .set(headers)
    .buffer()
    .end(function(res){
      var back,
        status,
        loginedUrl;

      if(res.ok && res.text){
        back = res.text.split("'");
        status = back[1];
        loginedUrl = back[5];

        if(status === '0'){//成功登录
          opt.backData.loginedUrl = loginedUrl;
        }else{
          opt.backData.loginErr = status;
          opt.backData.errorMsg = res.text;
        };

        console.log('login text : ' + res.text)

      };

      if(typeof opt.callback == 'function'){
        opt.callback();
      };

    });

};